<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getSourceListApi } from '@/api/source'

const total = ref(10)
const currentPage = ref(1)
const pageSize = ref(10)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
}

const formInline = reactive({
  user: '',
  region: '',
  date: '',
})

const onSubmit = () => {
  console.log('submit!')
}

const tableData = ref([])

onMounted(async () => {
  const formData =  new FormData()
  formData.append('current', currentPage)
  formData.append('size', pageSize)
  const res = await getSourceListApi(formData)
  console.log(res)
  tableData.value = res.data.list
  total.value = res.data.list.length
})
</script>

<template>
  <div style="position: relative; height: 100%">
    <div class="filter">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="密探名称">
          <el-input
            v-model="formInline.user"
            placeholder="请输入密探名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="Activity zone">
          <el-select
            v-model="formInline.region"
            placeholder="Activity zone"
            clearable
          >
            <el-option label="Zone one" value="shanghai" />
            <el-option label="Zone two" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="Activity time">
          <el-date-picker
            v-model="formInline.date"
            type="date"
            placeholder="Pick a date"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="border-radius: 8px; overflow: hidden">
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="角色名称（中文）" />
        <el-table-column prop="enname" label="角色名称（英文）" />
        <el-table-column prop="url" label="资源路径" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="created" label="创建时间">
          <template v-slot="{ row }">
            <div>
              {{ row.created.substring(0, 19).replace('T', ' ') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="updated" label="修改时间">
          <template v-slot="{ row }">
            <div>
              {{ row.updated.substring(0, 19).replace('T', ' ') }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      v-model:total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style lang="less" scoped>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.filter {
  .el-form {
    display: flex;
    justify-content: start;
  }
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.el-pagination {
  position: absolute;
  bottom: 15px;
  transform: translateX(-50%);
  left: 50%;
}
</style>
